# -*- python -*-
# ex: set syntax=python:
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# READ THIS:
# See http://dev.chromium.org/developers/testing/chromium-build-infrastructure

import os
import socket

# These modules come from scripts, which must be in the PYTHONPATH.
from master import master_utils
from master import slaves_list
from master.factory import remote_run_factory
from master.try_job_gerrit import TryJobGerritScheduler
from master.try_job_gerrit import TryJobGerritStatus

import config
import master_site_config

ActiveMaster = master_site_config.TryServerANGLE

def m_remote_run_chromium_src(recipe, **kwargs):
  return remote_run_factory.RemoteRunFactory(
      active_master=ActiveMaster,
      repository='https://chromium.googlesource.com/chromium/tools/build.git',
      recipe=recipe,
      factory_properties={'path_config': 'kitchen'},
      **kwargs)

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

# Disable merge requests because this is a tryserver
c['mergeRequests'] = False

config.DatabaseSetup(c)

####### CHANGESOURCES

c['change_source'] = []

####### BUILDERS

c['builders'] = []

# ANGLE Android bots
c['builders'].append({
  'name': 'android_angle_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'android',
})

# ANGLE linux bots
c['builders'].append({
  'name': 'linux_angle_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'linux',
})
c['builders'].append({
  'name': 'linux_angle_dbg_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'linux',
})
c['builders'].append({
  'name': 'linux_angle_chromeos_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'linux_chromeos',
})

# ANGLE mac bots
c['builders'].append({
  'name': 'mac_angle_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'mac',
})
c['builders'].append({
  'name': 'mac_angle_dbg_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'mac',
})

# ANGLE win bots
c['builders'].append({
  'name': 'win_angle_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'win',
})
c['builders'].append({
  'name': 'win_angle_dbg_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'win',
})
c['builders'].append({
  'name': 'win_angle_x64_rel_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'win',
})
c['builders'].append({
  'name': 'win_angle_x64_dbg_ng',
  'factory': m_remote_run_chromium_src('chromium_trybot', timeout=3600),
  # Share build directory with compatible existing builders to save space.
  'slavebuilddir': 'win',
})

slaves = slaves_list.SlavesList('slaves.cfg', 'TryServerANGLE')
for builder in c['builders']:
  # Associate the slaves to the builders. The configuration is in slaves.cfg.
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])
  # Don't enable auto_reboot for people testing locally.
  builder.setdefault('auto_reboot', ActiveMaster.is_production_host)


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### SCHEDULERS

# Configure the Schedulers;
# Main Tryscheduler for the try queue. groups is defined in the loop above.
c['schedulers'] = []

c['schedulers'].append(TryJobGerritScheduler(
    name='angle-gerrit',
    default_builder_names=[
        'android_angle_rel_ng',
        'linux_angle_rel_ng',
        'linux_angle_dbg_ng',
        'mac_angle_rel_ng',
        'mac_angle_dbg_ng',
        'win_angle_rel_ng',
        'win_angle_dbg_ng',
        'win_angle_x64_rel_ng',
    ],
    gerrit_host=ActiveMaster.gerrit_host,
    gerrit_projects=['angle/angle'],
    pollInterval=60))

####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
if ActiveMaster.is_production_host:
  c['buildbotURL'] = ActiveMaster.buildbot_url
else:
  c['buildbotURL'] = 'http://%s:%d/' % (
      socket.getfqdn(), ActiveMaster.master_port)

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster,
    public_html='../master.tryserver.chromium.linux/public_html',
    templates=['../master.tryserver.chromium.linux/templates'])

c['status'].append(TryJobGerritStatus(
    ActiveMaster.gerrit_host))

# The followings are what is kept on disk.
c['buildHorizon'] = 500
c['logHorizon'] = 250
# Must be at least 2x the number of slaves.
c['eventHorizon'] = 200
c['logCompressionLimit'] = False

# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3

####### PROJECT IDENTITY

# The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.
c['projectURL'] = 'http://dev.chromium.org/developers/testing/try-server-usage'
